﻿using System;
using System.Linq;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Text;
using System.Xml;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Reviewer.Entities.Services;
using Reviewer.Business;
using Reviewer.Entities;

public partial class Partner_Pricerunner : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (IReviewService reviewService = ServiceFactory.ReviewService)
            using (IGameService gameService = ServiceFactory.GameService)
            using (IMetadataService<Format> formatService = ServiceFactory.GetMetadataService<Format>())
            using (IMetadataService<Genre> genreService = ServiceFactory.GetMetadataService<Genre>())
            using (IWriterService writerService = ServiceFactory.WriterService)
            {
                XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
                writer.Formatting = Formatting.None;
                writer.WriteStartDocument();
                writer.WriteStartElement("Products");
                foreach (Review review in reviewService.Find().OrderByDescending(t => t.Id))
                {
                    Game game = gameService.GetBy(review.GameId);
                    if (game == null)
                    {
                        continue;
                    }

                    Format format = formatService.GetAll().FirstOrDefault(t => t.Id.Equals(game.Format));
                    Genre genre = genreService.GetAll().FirstOrDefault(t => t.Id.Equals(game.Genre));
                    Writer author = writerService.Find(review.Author);

                    writer.WriteStartElement("Product");
                    writer.WriteElementString("SKU", review.Id.ToString());
                    writer.WriteElementString("Category", "Spil: " + (format == null ? "Ukendt format" : format.Name));
                    writer.WriteElementString("ProductName", game.Title);
                    writer.WriteElementString("SubjectOfReview", game.Title);
                    writer.WriteElementString("URL", "http://www.reviewer.dk/" + UrlUtillity.GetReviewUri(review));
                    writer.WriteElementString("Summary", review.Header);
                    writer.WriteElementString("Grade", review.Rating.ToString());
                    writer.WriteElementString("MaxGrade", "6");
                    writer.WriteElementString("Author", (author == null ? "Ukendt forfatter" : author.LogOnName));
                    writer.WriteElementString("Date", review.LastModifiedDate.ToString());
                    writer.WriteEndElement();
                }
                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Flush();
                writer.Close();
            }
        }
    }
}
